package zzzhc.image.recognize.nn;

import org.joone.io.MemoryOutputSynapse;
import org.joone.net.NeuralNet;

import zzzhc.image.recognize.ImageToken;
import zzzhc.image.recognize.TokenRecognizer;

public class NNRecognizer implements TokenRecognizer {

	private static final long serialVersionUID = 4809920425575896454L;

	private NeuralNet net;

	private TrainResult trainResult;

	public Character recognize(ImageToken token) {
		NeuralNet nn = net.cloneNet();
		nn.removeAllInputs();
		nn.removeAllOutputs();
		NNInputItem item = NNInputItem.fromImageToken(token);
		NNInput input = new NNInput();
		input.addItem(item);
		nn.addInputSynapse(input.createInputSynapse());

		MemoryOutputSynapse out = new MemoryOutputSynapse();
		nn.addOutputSynapse(out);

		nn.start();
		nn.getMonitor().Go();

		double[] data = out.getNextPattern();
		NNOutputItem outputItem = new NNOutputItem();
		outputItem.setData(data);
		return trainResult.getChar(outputItem);
	}

	public TrainResult getTrainResult() {
		return trainResult;
	}

	public void setTrainResult(TrainResult trainResult) {
		this.trainResult = trainResult;
		this.net = trainResult.getNeuralNet();
	}

}
